package com.example.aidl_test.client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.example.aidl_test.common.Book;
import com.example.aidl_test.common.IBookManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "aidl_test_client";
    private TextView mTextView;
    private final List<Book> mList = new ArrayList<>();
    private IBookManager mBookManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button b1 = findViewById(R.id.bind);
        b1.setOnClickListener(v -> bind());
        Button b2 = findViewById(R.id.unbind);
        b2.setOnClickListener(v -> unbind());
        Button b3 = findViewById(R.id.get_list);
        b3.setOnClickListener(v -> getList());
        Button b4 = findViewById(R.id.add_book);
        b4.setOnClickListener(v -> addBook());

        mTextView = findViewById(R.id.text_view);
    }

    private final ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG,"onServiceConnected");
            mBookManager = IBookManager.Stub.asInterface(service);
            getList();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG,"onServiceDisconnected");
            mBookManager = null;
        }
    };
    private void bind() {
        Log.i(TAG,"bind");
        Intent i = new Intent();
        i.setClassName("com.example.aidl_test.server",
                "com.example.aidl_test.server.MyService");
        bindService(i, mConn, Context.BIND_AUTO_CREATE);
    }
    private void unbind() {
        Log.i(TAG,"unbind");
        unbindService(mConn);
    }
    
    private void getList() {
        if (mBookManager == null) return;
        try {
            List<Book> bookList = mBookManager.getBookList();
            updateList(bookList);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }

    }
    private void addBook() {
        if (mBookManager == null) return;
        int id = mList.isEmpty() ? 1 : mList.get(mList.size() - 1).getID() + 1;
        try {
            mBookManager.addBook(new Book(id, "Book" + id));
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    private void updateList(List<Book> bookList) {
        if (bookList != null && !bookList.isEmpty()) {
            mList.clear();
            mList.addAll(bookList);

            StringBuilder sb = new StringBuilder();
            for (Book b : mList) {
                sb.append(b.toString()).append("\n");
            }
            mTextView.setText(sb.toString());
        }
    }
}